var settings = @options("settings") || {};

@lab(function (params) {
    return @.async(async function(){
        await @promise("slime.subscribe.ready");
    }).all(@.keys(settings),function (ou) {
        lab.slime.core.@initSession({},ou).then(function(currentOU) {
            return lab.slime_subscribe.core.@getSettings(currentOU.id, settings[ou]);
        }).pipe(this);
    });
}).rejected(function (error) {
    @error(error);
})


module.exports = function (lab) {
    return {
        @getSettings : function (ouid,defaultValue) {
            return lab.find("slime_subscribe_settings",ouid).then(function (settings) {
                if (settings) {
                    if (defaultValue) {
                        lab.update("slime_subscribe_settings",settings,defaultValue).pipe(this);
                    } else {
                        this.next(settings);
                    }
                } else {
                    lab.create("slime_subscribe_settings",@.merge.simple({
                        priority : []
                    },defaultValue,{
                        create_date : Date.now(),
                        id : ouid, 
                    })).pipe(this);
                }
            })
        }
    }
}